//
//  LWMeituanRefresh.swift
//  刷新控件
//
//  Created by kobe on 2016/12/29.
//  Copyright © 2016年 kobe. All rights reserved.
//

import UIKit

class LWMeituanRefresh: LWRefreshView {

    
    @IBOutlet weak var buildingIconView: UIImageView!
    
    @IBOutlet weak var earthIconView: UIImageView!
    
    @IBOutlet weak var kanggarooIconView: UIImageView!
    
    override var parentViewHeight: CGFloat {
        didSet {
            print("父视图高度 \(parentViewHeight)")
            
            if parentViewHeight < 23 {
                return
            }
            // 23 - 126
            // 0.4 - 1.0
            var scale: CGFloat
            if parentViewHeight > 126 {
                scale = 1
            } else {
                scale = 1 - (126 - parentViewHeight) / (126 - 23)
            }
            kanggarooIconView.transform = CGAffineTransform(scaleX: scale, y: scale)
            
        }
    }
    
    override func awakeFromNib() {
        // 1.房子
        let bImage1 = #imageLiteral(resourceName: "icon_building_loading_1")
        let bImage2 = #imageLiteral(resourceName: "icon_building_loading_2")
        
        buildingIconView.image = UIImage.animatedImage(with: [bImage1,bImage2], duration: 0.5)
        
        // 2.地球
        let animation = CABasicAnimation(keyPath: "transform.rotation")
        animation.toValue = -2 * M_PI
        animation.repeatCount = MAXFLOAT
        animation.duration = 3
        animation.isRemovedOnCompletion = false
        earthIconView.layer.add(animation, forKey: nil)
        
        // 3.袋鼠
        kanggarooIconView.transform = CGAffineTransform(scaleX: 0.4, y: 0.4)
        // 设置锚点
        kanggarooIconView.layer.anchorPoint = CGPoint(x: 0.5, y: 1)
        // 设置center
        let x = self.bounds.width * 0.5
        let y = self.bounds.height - 23
        kanggarooIconView.center = CGPoint(x: x, y: y)
        
        let kImage1 = #imageLiteral(resourceName: "icon_small_kangaroo_loading_1")
        let kImage2 = #imageLiteral(resourceName: "icon_small_kangaroo_loading_2")
        
        kanggarooIconView.image = UIImage.animatedImage(with: [kImage1,kImage2], duration: 0.5)
        
        
    }

}
